import { Controller, Post, Body, Get, Query, Ip, Request, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { Public } from '../../decorators/public.decorator';
import {
  LoginDto,
  PhoneLoginDto,
  SmsLoginDto,
  RegisterDto,
  SendSmsDto,
  ResetPasswordDto,
} from './dto/auth.dto';
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';

@ApiTags('auth')
@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Public()
  @Post('login/password')
  @ApiOperation({ summary: '密码登录' })
  async passwordLogin(@Body() loginDto: LoginDto, @Ip() ip: string) {
    return this.authService.passwordLogin(loginDto, ip);
  }

  @Public()
  @Post('login/phone')
  @ApiOperation({ summary: '手机号登录' })
  async phoneLogin(@Body() phoneLoginDto: PhoneLoginDto, @Ip() ip: string) {
    return this.authService.phoneLogin(phoneLoginDto, ip);
  }

  @Public()
  @Post('login/sms')
  @ApiOperation({ summary: '验证码登录' })
  async smsLogin(@Body() smsLoginDto: SmsLoginDto, @Ip() ip: string) {
    return this.authService.smsLogin(smsLoginDto, ip);
  }

  @Public()
  @Post('register')
  @ApiOperation({ summary: '手机号注册' })
  async register(@Body() registerDto: RegisterDto) {
    return this.authService.register(registerDto);
  }

  @Public()
  @Post('password/reset')
  @ApiOperation({ summary: '重置密码' })
  async resetPassword(@Body() resetPasswordDto: ResetPasswordDto) {
    return this.authService.resetPassword(resetPasswordDto);
  }

  @Public()
  @Post('sms/send')
  @ApiOperation({ summary: '发送验证码' })
  async sendSms(@Body() sendSmsDto: SendSmsDto) {
    return this.authService.sendSms(sendSmsDto);
  }

  @Post('logout')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: '退出登录' })
  async logout(@Request() req) {
    // 这里可以添加一些逻辑，比如将 token 加入黑名单
    // 或者记录用户退出登录的时间
    console.log('用户退出登录:', req.user);
    
    return {
      message: '退出成功'
    };
  }
}

